package edu.kit.csl.cratyli.ui;

import edu.kit.csl.cratyli.control.CombinationOrderStrategy;
import edu.kit.csl.cratyli.control.IncrementalOrderStrategy;
import edu.kit.csl.cratyli.control.TreeOrderStrategy;
import edu.kit.csl.cratyli.datatypes.BadParameterValueException;

/**
 * This class is the main class. It contains the main() method which starts the
 * main program work flow (load resources, extract data, output results).
 * 
 * @see #main(String[])
 */
public class Main {

	/**
	 * Main runner method. Initializes the configuration singleton and 
	 * starts the main extraction loop.
	 * 
	 * @param args Use -help to display command line arguments
	 */
	public static void main(String[] args) {
		Logger log = Logger.getSingleton();
		// Load configuration
		Configuration conf = Configuration.getSingleton();
		if (args.length == 1 && args[0].equals("-help")) {
			conf.printHelp();
			return;
		}
		conf.processCommandLineArguments(args);
		if (log.isFatalErrorRegistered()) {
			log.fatalError("Fatal errors occurred. Use -help to show "
					+ "information about each available parameter");
			return;
		}
		log.notice("Configuration loaded.");
		try {
			CombinationOrderStrategy s = createOrderStrategy();
			s.startCombining();
		} catch (BadParameterValueException e) {
			Logger.getSingleton().fatalError("A bad parameter value aborted "
				+ "the extraction process: " + e.getMessage());
		}
	}
	
	/*
	 * Factory method for creating a combination order strategy.
	 */
	private static CombinationOrderStrategy createOrderStrategy()
			throws BadParameterValueException {
		String l = Configuration.getSingleton().getString("combinationOrder");
		if (l.equals("incremental")) {
			return new IncrementalOrderStrategy();
		} else if (l.equals("tree")) {
			return new TreeOrderStrategy();
		}
		throw new BadParameterValueException("Alignment loader " + l
				+ " is not implemented.");
	}
}
